using System.Collections.Generic;

namespace Core
{
    public partial class MapLevelConfigLoader
    {
        private readonly Dictionary<int, List<IConfig>> sceneDict = new Dictionary<int, List<IConfig>>();

        protected override void OnEndLoad()
        {
            foreach ((int key, IConfig config) in dict)
            {
                var sceneConfig = config as MapLevelConfig;
                if (sceneConfig == null)
                    continue;

                if (!sceneDict.TryGetValue(sceneConfig.sceneId, out var list))
                {
                    list = new List<IConfig>();
                    sceneDict[sceneConfig.sceneId] = list;
                }

                list.Add(config);
            }

            foreach(var list in sceneDict.Values)
            {
                list.Sort((a, b) => (a as MapLevelConfig).id.CompareTo((b as MapLevelConfig).id));
            }
        }

        public bool HashScene(int sceneId)
        {
            return sceneDict.ContainsKey(sceneId);
        }

        public MapLevelConfig GetConfig(int sceneId, int level)
        {
            if(sceneDict.TryGetValue(sceneId, out var config))
            {
                if(level < config.Count)
                {
                    return config[level] as MapLevelConfig;
                }
            }
            return default;
        }

        public bool IsNext(int sceneId, int level)
        {
            return GetConfig(sceneId, level) != null;
        }
    }
}